package com.graduation.sfstore.server;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/**
 *
 * 服务器端程序：
 *
 * 1. 监听一端口，等待客户接入；
 * 2. 一旦有客户接入，就构造一个Socket会话对象；
 * 3. 将这个会话交给线程处理，然后主程序继续监听。
 *
 * @author OKJohn
 * @version 1.0
 */


public class Server extends ServerSocket {
    private StringBuffer stringBuffer=new StringBuffer();


    private BufferedReader reader;

    public String returndata(){
        return stringBuffer.toString();
    }

    public void setStringBuffer(StringBuffer strs){
        this.stringBuffer = strs;
    }


    public Server(int serverPort) throws IOException {
//用指定的端口构造一个ServerSocket
        super(serverPort);
        try {
            while (true) {
//监听一端口，等待客户接入
                Socket socket = accept();
//将会话交给线程处理
                new ServerThread(socket);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            close();  //关闭监听端口
        }
    }

    // inner-class ServerThread
    class ServerThread extends Thread {
        private Socket socket;
        private DataInputStream in;
        private PrintWriter out;

        // Ready to conversation
        public ServerThread(Socket s) throws IOException {
            this.socket = s;
            // 构造该会话中的输入输出流
//            in = new BufferedReader(new InputStreamReader(
//                    socket.getInputStream()));
            in=new DataInputStream(socket.getInputStream());
            out = new PrintWriter(socket.getOutputStream(), true);
            start();
        }

        // Execute conversation
        public void run() {
            try {

                // Communicate with client until "bye" received.
                while (true) {
                    // 通过输入流接收客户端信息
                    byte c = in.readByte();
                    if (c==1008) {  // 是否终止会话
                        break;
                    }
                    stringBuffer.append((char)c);
                    if(stringBuffer.length()==7){
                        System.out.println("Received message:" + stringBuffer.toString());

                        reader = new BufferedReader(
                                new InputStreamReader(socket.getInputStream()));

                        setStringBuffer(stringBuffer);

                        stringBuffer=new StringBuffer();
                    }
                    String msg1 = "PA0";
                    String msg2 = "PA1";
                    String msg3 = "PB0";
                    String msg4 = "PB1";
                    int i = new Random().nextInt(4);
                    switch (i){
                        case 0:
                            // 通过输出流向客户端发送信息
                            out.print(msg1);
                            break;
                        case 1:
                            // 通过输出流向客户端发送信息
                            out.print(msg2);
                            break;
                        case 2:
                            // 通过输出流向客户端发送信息
                            out.print(msg3);
                            break;
                        case 3:
                            // 通过输出流向客户端发送信息
                            out.print(msg4);
                            break;
                        default:
                            // 通过输出流向客户端发送信息
                            out.print(msg4);
                            break;

                    }

                    out.flush();

                }

                out.close();
                in.close();
                socket.close();

            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }





    }

    // main method
//    public static void main(String[] args) throws IOException {
//        new Server(10000);
//    }
}
